package xworker.javafx.beans.property;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.lang.actions.ActionUtils;

public class FXObjectProperty {
    public static void create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        Object property = self.doAction("getProperty", actionContext);
        if(property == null){
            throw new NullPointerException("Property is null, thing=" + self.getMetadata().getPath());
        }
        actionContext.g().put(self.getMetadata().getName(), property);

        actionContext.peek().put("parent", property);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }
    }

    public static Object getProperty(ActionContext actionContext) throws Exception {
        Thing self = actionContext.getObject("self");
        String exp = self.getStringBlankAsNull("property");
        if(exp == null){
            return null;
        }else if(exp.contains(":")){
            return ActionUtils.getStringData(self, self,"property", actionContext);
        }else if(exp.contains(".")){
            String[] exps = exp.split("[.]");
            return PropertyFactory.getProperty(actionContext.getObject(exps[0].trim()), exps[1].trim());
        }else{
            return null;
        }
    }
}
